package ytdp.careagle.com.ytdp.activity;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.umeng.analytics.dplus.UMADplus;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;
import ytdp.careagle.com.ytdp.R;
import ytdp.careagle.com.ytdp.adapter.base.CommonAdapter;
import ytdp.careagle.com.ytdp.adapter.base.MultiItemTypeAdapter;
import ytdp.careagle.com.ytdp.adapter.base.ViewHolder;
import ytdp.careagle.com.ytdp.app.APP;
import ytdp.careagle.com.ytdp.app.manager.ImageLoadManager;
import ytdp.careagle.com.ytdp.entity.PushCarEntity;
import ytdp.careagle.com.ytdp.entity.QueryAllCarType;
import ytdp.careagle.com.ytdp.entity.SellCarProgramEntity;
import ytdp.careagle.com.ytdp.mvp.presenter.AllCarTypePresenter;
import ytdp.careagle.com.ytdp.mvp.view.IAllCarTypeView;

public class AllCarTypeActivity extends BaseActivity implements MultiItemTypeAdapter.OnItemClickListener, IAllCarTypeView {

    @Bind(R.id.tv_title)
    TextView tvTitle;
    @Bind(R.id.tv_subtitle)
    TextView tvSubtitle;
    @Bind(R.id.tv_back_homepage)
    TextView tvBackHomepage;
    @Bind(R.id.tv_lastPage)
    TextView tvLastPage;
    @Bind(R.id.recyclerView)
    RecyclerView recyclerView;
    private CommonAdapter<QueryAllCarType> adapter;
    private List<QueryAllCarType> list;
    private double myCarSellPrice;
    private AllCarTypePresenter presenter;
    private String valuationQueryRecordIds;

    @Override
    public int getLayoutId() {
        return R.layout.activity_all_car_type;
    }

    @Override
    public void initView() {
        UMADplus.track(this, "所有车型页面", null);
        Intent intent = getIntent();
        list = (List<QueryAllCarType>) intent.getSerializableExtra("data");
        valuationQueryRecordIds = intent.getStringExtra("valuationQueryRecordId");
        myCarSellPrice = intent.getDoubleExtra("sellPrice", 0);
        recyclerView.setLayoutManager(new GridLayoutManager(this, 3));
        adapter = new CommonAdapter<QueryAllCarType>(this, R.layout.item_push_car_all, list) {
            @Override
            protected void convert(ViewHolder holder, QueryAllCarType data, int position) {
                holder.setText(R.id.tv_item_title, data.getName());
                if (data.getSellPrice() == null || data.getSellPrice() == 0) {
                    holder.setText(R.id.tv_item_subtitle, "");
                } else {
                    holder.setText(R.id.tv_item_subtitle, data.getSellPrice() + "万元起售");
                }
                ImageView imageView = holder.getView(R.id.iv_item);
                ImageLoadManager.getImageLoadManager().load(data.getCoverImage(), imageView);
            }
        };
        recyclerView.setAdapter(adapter);
        adapter.setOnItemClickListener(this);
        presenter = new AllCarTypePresenter(this);
        tvSubtitle.setText("总有一款适合你（共" + list.size() + "个车系）");
    }

    @OnClick({R.id.tv_back_homepage, R.id.tv_lastPage})
    public void onClick(View view) {
        APP.getInstance().resetCountDown();
        switch (view.getId()) {
            case R.id.tv_back_homepage:
                APP.getInstance().backHomeActivity();
                break;
            case R.id.tv_lastPage:
                finish();
                break;
        }
    }

    @Override
    public void onItemClick(View view, RecyclerView.ViewHolder holder, int position) {
        QueryAllCarType queryAllCarType = list.get(position);
        UMADplus.track(this, "广告" + queryAllCarType.getName(), null);
        presenter.findVehicleByVehicleSeriesAndOnline(this, queryAllCarType.getId() + "");
        APP.getInstance().resetCountDown();
    }

    @Override
    public boolean onItemLongClick(View view, RecyclerView.ViewHolder holder, int position) {
        return false;
    }

    @Override
    public void setCarData(List<SellCarProgramEntity> data) {
        Intent intent = new Intent(this, FinancialProgramActivity.class);
        intent.putExtra("data", (Serializable) data);
        intent.putExtra("sellPrice", myCarSellPrice);
        intent.putExtra("valuationQueryRecordId", valuationQueryRecordIds);
        startActivity(intent);
    }
}
